package com.ruoyi.question.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.DtCategory;
import com.ruoyi.question.service.IDtCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类（如历史，政治）Controller
 *
 * @author llc
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/question/category")
public class DtCategoryController extends BaseController {
    @Autowired
    private IDtCategoryService dtCategoryService;

    /**
     * 查询分类（如历史，政治）列表
     */
    @PreAuthorize("@ss.hasPermi('question:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtCategory dtCategory) {
        startPage();
        List<DtCategory> list = dtCategoryService.selectDtCategoryList(dtCategory);
        return getDataTable(list);
    }

    /**
     * 导出分类（如历史，政治）列表
     */
    @PreAuthorize("@ss.hasPermi('question:category:export')")
    @Log(title = "分类（如历史，政治）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtCategory dtCategory) {
        List<DtCategory> list = dtCategoryService.selectDtCategoryList(dtCategory);
        ExcelUtil<DtCategory> util = new ExcelUtil<DtCategory>(DtCategory.class);
        util.exportExcel(response, list, "分类（如历史，政治）数据");
    }

    /**
     * 获取分类（如历史，政治）详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(dtCategoryService.selectDtCategoryById(id));
    }

    /**
     * 新增分类（如历史，政治）
     */
    @PreAuthorize("@ss.hasPermi('question:category:add')")
    @Log(title = "分类（如历史，政治）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DtCategory dtCategory) {
        return toAjax(dtCategoryService.insertDtCategory(dtCategory));
    }

    /**
     * 修改分类（如历史，政治）
     */
    @PreAuthorize("@ss.hasPermi('question:category:edit')")
    @Log(title = "分类（如历史，政治）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtCategory dtCategory) {
        return toAjax(dtCategoryService.updateDtCategory(dtCategory));
    }

    /**
     * 删除分类（如历史，政治）
     */
    @PreAuthorize("@ss.hasPermi('question:category:remove')")
    @Log(title = "分类（如历史，政治）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dtCategoryService.deleteDtCategoryByIds(ids));
    }
}
